package xyz.luhui1hao.sell.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.luhui1hao.sell.domain.ProductInfo;
import xyz.luhui1hao.sell.dto.CartItemDTO;
import xyz.luhui1hao.sell.enums.ProductStatusEnum;
import xyz.luhui1hao.sell.enums.ResultEnum;
import xyz.luhui1hao.sell.exception.SellException;
import xyz.luhui1hao.sell.mapper.ProductInfoMapper;
import xyz.luhui1hao.sell.service.ProductInfoService;

import java.util.List;

/**
 * @ClassName ProductInfoServiceImpl
 * @Description TODO
 * @Author luhui1hao
 * @Date 2018/8/20 上午10:42
 * @Version 1.0
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoMapper profuctInfoMapper;

    @Override
    public ProductInfo findOne(String productId) {
        return profuctInfoMapper.findOne(productId);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return profuctInfoMapper.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public List<ProductInfo> findAll(int page, int size) {
        PageHelper.startPage(page, size);
        List<ProductInfo> productInfoList = profuctInfoMapper.findAll();
        return productInfoList;
    }

    @Override
    public List<ProductInfo> findByProductStatus(Integer productStatus) {
        return profuctInfoMapper.findByProductStatus(productStatus);
    }

    @Override
    public int insertProductInfo(ProductInfo productInfo) {
        return profuctInfoMapper.insertProductInfo(productInfo);
    }

    @Override
    public int updateProductInfo(ProductInfo productInfo) {
        return profuctInfoMapper.updateProductInfo(productInfo);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartItemDTO> cartItemDTOList) {
        //遍历集合
        for (CartItemDTO cartItemDTO : cartItemDTOList) {
            //根据商品id查找商品
            ProductInfo productInfo = profuctInfoMapper.findOne(cartItemDTO.getProductId());
            if(productInfo == null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //计算加库存
            Integer result = productInfo.getProductStock() + cartItemDTO.getProductQuantity();
            //更新库存
            productInfo.setProductStock(result);
            profuctInfoMapper.updateProductInfo(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartItemDTO> cartItemDTOList) {
        for (CartItemDTO cartItemDTO : cartItemDTOList) {
            ProductInfo productInfo = profuctInfoMapper.findOne(cartItemDTO.getProductId());
            if(productInfo == null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer result = productInfo.getProductStock() - cartItemDTO.getProductQuantity();
            //如果扣完库存小于0，扣库存失败
            if(result < 0){
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            //如果扣完库存不小于0，扣库存成功
            productInfo.setProductStock(result);
            //更新库存
            profuctInfoMapper.updateProductInfo(productInfo);
        }
    }
}
